package com.senven.api.controller;

import com.senven.api.annotation.LogInfo;
import com.senven.api.commom.ResponseCode;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.dao.RoleMapper;
import com.senven.api.exception.GlobalException;
import com.senven.api.pojo.Account;
import com.senven.api.pojo.Role;
import com.senven.api.pojo.Student;
import com.senven.api.service.AccountService;
import com.senven.api.service.RoleService;
import com.senven.api.service.StudentService;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author LQP
 */
@RequestMapping("/api/admin")
@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private StudentService studentService;


    @GetMapping("/getAccountListById")
    @ResponseBody
    public Account queryAccountById(@RequestParam("id")int id){
        return accountService.queryAccountById(id);
    }

    /**
     * 获取管理员信息
     * @param s
     * @return
     */
    @GetMapping("/getAccountListByName")
    @ResponseBody
    public ResponsePage<Account> queryAccount(@RequestParam("name") String s){
        System.out.println(s);
      return accountService.queryAccount(s);
    }

    /**
     * 获取角色信息
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getRoleList")
    @ResponseBody
    public ResponsePage<Role> queryRoleList(@RequestParam(value = "current",defaultValue = "1") int current,
                                           @RequestParam(value = "size", defaultValue = "10") int size){
        return roleService.queryRoleList(current,size);
    }

    /**
     * 获取管理员列表
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAccountList")
    @ResponseBody
    public ResponsePage<Account> queryAccountList(@RequestParam(value = "current",defaultValue = "1") int current,
                                                  @RequestParam(value = "size", defaultValue = "10")int size){
        return accountService.queryAccountList(current,size);
    }

    /**
     * 删除一个社团管理员
     * @param id
     * @return
     */
    @GetMapping("/deleteAccount")
    @ResponseBody
    @LogInfo(operateType = "管理员业务",operateInfo = "删除账户")
    public ResponseResult deleteOrganUser(@RequestParam("id") int id){
        try {
            int result = accountService.deleteAccount(id);
            if (result == 0) {
                return ReponseResultUtil.error(-1, "账户删除失败");
            } else {
                return ReponseResultUtil.success("账户删除成功");
            }
        }catch (Exception e){
            throw new GlobalException(ResponseCode.ERROR.getCode(),"数据库错误");
        }
    }

    /**
     * 插入一个社团管理员
     * @param account
     * @return
     */
    @PostMapping("/insertAccount")
    @ResponseBody
    @LogInfo(operateType = "管理业务",operateInfo = "添加账户")
    public ResponseResult insertOrganUser(@RequestBody Account account){
        PasswordEncoder password = new BCryptPasswordEncoder();
        //System.out.println(account.getPassword());
        account.setPassword(password.encode(account.getPassword()));

        //检查有无重复
        ResponsePage<Account> res = accountService.queryAccount(account.getAccount());
        if(!res.getData().isEmpty() || res.getData().size() != 0){
            return ReponseResultUtil.error(-1,"请勿重复添加");
        }

        //同时插入学生
        Student stu = new Student();
        stu.setId(account.getAccount());
        stu.setSex(account.getSex());
        stu.setPhone(account.getPhone());
        studentService.insertStudent(stu);

        int result = accountService.insertAccount(account);
        if (result == 0){
            return ReponseResultUtil.error(-1,"账户插入失败");
        }else{
            return ReponseResultUtil.success("账户插入成功");
        }
    }

    /**
     * 插入一个社团管理员
     * @param account
     * @return
     */
    @PostMapping("/updateAccount")
    @ResponseBody
    @LogInfo(operateType = "管理业务",operateInfo = "更新管理员")
    public ResponseResult updateOrganUser (@RequestBody Account account){
        int result = accountService.updateAccount(account);
        if (result == 0){
            return ReponseResultUtil.error(-1,"管理员更新失败");
        }else{
            return ReponseResultUtil.success("管理员更新成功");
        }
    }

    @PostMapping("/resetPasswd")
    @ResponseBody
    @LogInfo(operateType = "管理业务",operateInfo = "重置密码")
    public ResponseResult resetPasswd(@RequestBody Account account, HttpServletRequest request){
        HttpSession session = request.getSession();
        String ac = (String) session.getAttribute("account");
        //String oldPass = (String)session.getAttribute("password");
        if(accountService.resetPasswd(account.getPhone(),account.getPassword(),ac)){
            return ReponseResultUtil.success("密码重置成功");
        }else {
            return ReponseResultUtil.error(-1,"密码重置失败");
        }
    }

}
